package com.xianrenit.xianrenpuzi.manager.service.impl;

import com.xianrenit.xianrenpuzi.manager.mapper.SysUserRoleMapper;
import com.xianrenit.xianrenpuzi.manager.service.SysUserRoleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Resource
    SysUserRoleMapper sysUserRoleMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void assignRoles2User(Long userId, List<Long> roleIds) {
        //1、删除userId他所有的已分配角色：物理删除
        sysUserRoleMapper.deleteRolesByUserId(userId);
        //2、角色id不为空   将roleIds分配给userId
        if(!CollectionUtils.isEmpty(roleIds)){
            sysUserRoleMapper.assignRoles2User(userId,roleIds);
        }

    }
}
